package org.xukai.remoting.ws.jsonsoap;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import java.util.List;

/**
 * @author xukai
 * @desc
 * @date 2016-10-08-上午 9:11
 */
public class HeardInterceptor extends AbstractSoapInterceptor {


    public HeardInterceptor() {
        super(Phase.WRITE);
    }

    @Override
    public void handleMessage(SoapMessage soapMessage) throws Fault {
        QName qName=new QName("http://www.ns2.com");
        Document doc = DOMUtils.createDocument();
        Element root = doc.createElement("licenseInfo");

        Element username = doc.createElement("username");
        username.setTextContent("test");

        Element password = doc.createElement("password");
        password.setTextContent("test");


        root.appendChild(username);
        root.appendChild(password);
        SoapHeader header = new SoapHeader(qName, root);
        // 获取SOAP消息的全部头
        List<Header> headers = soapMessage.getHeaders();
        headers.add(header);
    }
}
